<template>
  <div class="app-container">
    <el-dialog :visible="remarkProptVisible" title="备注" @close="closeDialog">
      <ul class="remark_ul">
        <li v-for="(item, index) in remarklistIn" :key="index">
          <div class="title">
            <span v-if="item.uid" class="mr10">
              <span class="f_333">操作人:</span>
              <span>{{ item.uid | userNameStr }}</span>
            </span>
            <span class="mr10">
              <span class="f_333">操作时间:</span>
              <span class="{background: red}">{{ item.createTime }}</span>
            </span>
            <span v-if="item.isPay >= 0" class="mr10">
              <span class="f_333">订单状态:</span>
              <span>{{ item.isPay | isPayStr }}</span>
            </span>
            <span v-if="item.orderStatus >= 0" class="mr10">
              <span class="f_333">订单状态:</span>
              <span>{{ item.orderStatus | orderStatus }}</span>
            </span>
          </div>
          <p>
            <span class="f_333 remark">备注：</span>{{ item.remark }}
          </p>
        </li>
      </ul>
      <div class="remarkbox">
        <el-form ref="remarkForm" :model="remarkForm" :rules="rules">
          <el-form-item label="请输入备注" prop="remarkText" >
            <el-input v-model="remarkForm.remarkText" maxlength="200" placeholder="请输入备注"/>
          </el-form-item>
        </el-form>
        <!-- <p>请输入备注</p>
        <el-input v-model="remarkText" type="text" /> -->
      </div>
      <div class="btns">
        <el-button type="info" @click="cancelRemark">取消</el-button>
        <el-button v-preventReClick type="primary" @click="confirmRemark">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRemarkPage, addRemark } from '@/api/order'

import { getStoreRemarkPage, addStoreRemark, getGoodsRemarkPage, addGoodsRemark } from '@/api/store'

let bid;
export default {
  filters: {
    userNameStr(num) {
      const userNameMap = {
        1: 'admin',
        2: 'sale',
        3: 'casher'
      }
      return userNameMap[num]
    },
    isPayStr(num) {
      const isPayMap = {
        0: '未付款',
        1: '已付款'
      }
      return isPayMap[num]
    },
    // <span v-if="scope.row.orderStatus === -100">无效订单(订单取消)</span>
    //       <span v-else-if="scope.row.orderStatus === 0">未付款</span>
    //       <span v-else-if="scope.row.orderStatus === 100">待发货(已付款)</span>
    //       <span v-else-if="scope.row.orderStatus === 200">已发货(待收货)</span>
    //       <span v-else-if="scope.row.orderStatus === 300">已收货</span>
    //       <span v-else-if="scope.row.orderStatus === 400">申请退款</span>
    //       <span v-else-if="scope.row.orderStatus === 410">拒绝退款</span>
    //       <span v-else-if="scope.row.orderStatus === 420 || scope.row.orderStatus === 430 || scope.row.orderStatus === 440">退货中</span>
    //       <!-- <span v-else-if="scope.row.orderStatus === 420">待发货(退货中)</span>
    //       <span v-else-if="scope.row.orderStatus === 430">已发货(退货中)</span>
    //       <span v-else-if="scope.row.orderStatus === 440">已收货(退货中)</span> -->
    //       <span v-else-if="scope.row.orderStatus === 450">已退款</span>
    //       <span v-else-if="scope.row.orderStatus === 500">已完成</span>
    orderStatus(num) {
      const orderMap = {
        0: '未付款',
        100: '待发货(已付款)',
        200: '已发货(待收货)',
        300: '已收货',
        400: '申请退款',
        410: '拒绝退款',
        450: '已退款',
        500: '已完成'
      }
      return num < 0 ? '无效订单(订单取消)' : orderMap[num]
    }
  },
  props: {
    remarkProptList: {
      type: Array,
      required: true
    },
    remarkProptVisible: {
      type: Boolean,
      required: true
    },
    remarkProptIndex: {
      type: Number,
      required: true
    },
    remarkType: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      uid: '',
      vipOrerId: '',
      remarklistIn: [],
      remarkForm: {
        remarkText: ''
      },
      goodsOrderId: '',
      rules: {
        remarkText: [
          { required: true, message: '请输入备注', trigger: 'blur' },
          { max: 200, message: '最多输入200个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    remarkProptIndex(val) {
      console.log('--->', this.$props.remarkProptList)
      this.remarkProptIndex = val
      this.remarklistIn = this.$props.remarkProptList
      console.log(this.remarklistIn)
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:remarkProptVisible', false)
      this.$emit('update:remarkProptIndex', -1)
    },
    request(uid, vipOrderId) { // 会员订单的列表获取
      console.log('vipOrderId:', vipOrderId)
      this.uid = uid;
      this.vipOrderId = vipOrderId
      getRemarkPage({ vipOrderId }).then(res => {
        console.log('getRemarkPage:', res)
        if (res.code === '200') {
          this.remarklistIn = res.data.list
          console.log('会员订单:', res.data.list)
        }
      })
    },
    getGoodsRemark(goodsOrderId) { // 商城订单的列表获取
      const data = { goodsOrderId }
      this.goodsOrderId = goodsOrderId
      getGoodsRemarkPage(data).then(res => {
        console.log(res.data.list)
        this.remarklistIn = res.data.list
      })
    },
    confirmRemark() {
      this.$refs.remarkForm.validate((valid) => {
        if (valid) {
          if (this.remarkType === 'order') { // 会员订单的备注
            const data = { uid: this.uid, remark: this.remarkForm.remarkText, vipOrderId: this.vipOrderId, isPay: this.remarkProptList[0].isPay }
            console.log(data)
            addRemark(data).then(res => {
              // console.log(res)
              if (res.code === '200') {
                this.$message({
                  type: 'success',
                  message: '备注修改成功'
                });
              }
            })
          }
          if (this.remarkType === 'storeOrder') { // 商城订单的备注
            const data = { goodsOrderId: this.goodsOrderId, remark: this.remarkForm.remarkText }
            addGoodsRemark(data).then(res => {
              if (res.code === '200') {
                this.$message({
                  type: 'success',
                  message: '备注修改成功'
                });
              }
            })
          }
          this.$emit('update:remarkProptVisible', false)
          this.$emit('remarkText', this.remarkForm.remarkText)

          //   if (this.cashType !== -1) {
          //     const data = { id: bid, remark: this.remarkForm.remarkText, type: this.cashType } // type1. 提现 2.打款
          //     addRemark(data).then(res => {
          //       if (res.code === '200') {
          //         this.$message({
          //           type: 'success',
          //           message: '备注修改成功'
          //         });
          //       }
          //       this.$emit('update:remarkProptVisible', false)
          //       this.$emit('remarkText', this.remarkForm.remarkText)
          //     })
          //   } else {
          //     const data = { id: bid, remark: this.remarkForm.remarkText }
          //     // 提交备注
          //     upDateCashRecorde(data).then(res => {
          //       if (res.code === '200') {
          //         console.log(res)
          //         this.$message({
          //           type: 'success',
          //           message: '备注修改成功'
          //         });
          //       } else {
          //         this.$message({
          //           type: 'success',
          //           message: res.message
          //         });
          //       }
          //       this.$emit('update:remarkProptVisible', false)
          //       this.$emit('remarkText', this.remarkForm.remarkText)
          //     })
          //   }
          this.remarkForm.remarkText = ''
        }
      })
    },
    cancelRemark() {
      this.$emit('update:remarkProptVisible', false)
      this.$message({
        type: 'info',
        message: '取消修改备注'
      });
    }
  }
}
</script>

<style lang="scss" scoped>
  .remark_ul {
    margin: 0;
    padding: 0;
    width: 100%;
    box-sizing: border-box;
    padding: 0 20px;
    li {
      width: 100%;
      margin: 0;
      padding: 0;
      width: 100%;
      border-bottom: 1px solid #eee;
      padding-top: 20px;
      .title{
          padding:10px 6px;
      }
    }
  }
  .el-dialog__body{
    padding: 0;
  }
  .remarkbox{
    padding: 10px 20px;
    color:#666;
  }
  .remark{
      margin-top: 10px;
      margin-left: 6px;
  }
  .btns{
    padding: 20px;
    text-align: right;
  }
  .f_333{
      color: #333;
  }
  .bold{
      font-weight: bold;
  }
  .mr10{
      margin-right: 20px;
  }
  .f_blue{
      color:#409EFF
  }
</style>
